package com.atguigui.leetcode1;

/**
 * 1669.合并两个链表
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/1/30 9:05
 */
public class P1669MergeInBetweenLinkedLists {

    public static void main(String[] args) {
        Solution solution = new P1669MergeInBetweenLinkedLists().new Solution();
        // TO TEST
    }

    class Solution {
        public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
            ListNode preA = list1;

            //循环到a-1
            for (int i = 0; i < a - 1; i++) {
                preA = preA.next;
            }

            //删除的节点
            ListNode preB = preA;
            for (int i = 0; i < b - a + 2; i++) {
                preB = preB.next;
            }

            preA.next = list2;
            while (list2.next != null) {
                list2 = list2.next;
            }
            //续上右半段链表
            list2.next = preB;

            return list1;
        }
    }

    class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
